import { Vector3 } from 'three'
import TWEEN from 'three/addons/libs/tween.module.js'

//相机移动实现漫游等动画
function cameraControlsTwwen({ camera, controls, newP, newT, time = 2000, callBack } = {}) {
	const tween = new TWEEN.Tween({
		x1: camera.position.x, // 相机x
		y1: camera.position.y, // 相机y
		z1: camera.position.z, // 相机z
		x2: controls.target.x, // 控制点的中心点x
		y2: controls.target.y, // 控制点的中心点y
		z2: controls.target.z // 控制点的中心点z
	})
	tween.to(
		{
			x1: newP.x,
			y1: newP.y,
			z1: newP.z,
			x2: newT.x,
			y2: newT.y,
			z2: newT.z
		},
		time
	)
	tween.onUpdate(function (object) {
		camera.position.x = object.x1
		camera.position.y = object.y1
		camera.position.z = object.z1
		controls.target.x = object.x2
		controls.target.y = object.y2
		controls.target.z = object.z2
		controls.update()
	})
	tween.onComplete(function () {
		controls.enabled = true
		callBack?.()
	})
	tween.easing(TWEEN.Easing.Cubic.InOut)
	tween.start()
}

//相机移动实现漫游等动画
function cameraTwwen({ camera, newP, newT, time = 2000, callBack } = {}) {
	const distance = camera.position.length()
	const { x, y, z } = camera.position
	const direction = camera.getWorldDirection(new Vector3())
	const lookAt = new Vector3(x, y, z).add(direction.multiplyScalar(distance))

	const tween = new TWEEN.Tween({
		x1: camera.position.x, // 相机x
		y1: camera.position.y, // 相机y
		z1: camera.position.z, // 相机z
		x2: lookAt.x, // 相机lookAt
		y2: lookAt.y, // 相机y
		z2: lookAt.z // 相机z
	})
	tween.to(
		{
			x1: newP.x,
			y1: newP.y,
			z1: newP.z,
			x2: newT.x,
			y2: newT.y,
			z2: newT.z
		},
		time
	)
	tween.onUpdate(function (object) {
		camera.position.x = object.x1
		camera.position.y = object.y1
		camera.position.z = object.z1
		camera.lookAt(object.x2, object.y2, object.z2)
	})
	tween.onComplete(function () {
		callBack?.()
	})
	tween.easing(TWEEN.Easing.Cubic.InOut)
	tween.start()
}

export { cameraControlsTwwen, cameraTwwen, TWEEN }
